﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using VoicePhone.Common;

namespace VoicePhone.Utils
{
    public class CallingRecords
    {
        public ObservableCollection<CallingRecord> CallingList { get; set; } = new ObservableCollection<CallingRecord>();
    }

    public class CallingRecord : DependencyObject
    {
        System.Timers.Timer timer = new System.Timers.Timer() { Interval = 1000 };

        public CallingRecord()
        {
            timer.Elapsed += Timer_Elapsed;
            timer.Start();
        }

        ~CallingRecord()
        {
            timer.Stop();
            timer.Elapsed -= Timer_Elapsed;
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(() => { this.Duration++; }));
        }

        private string m_TimeStart;
        public string TimeStart
        {
            get { return m_TimeStart; }
            set
            {
                if (!string.IsNullOrWhiteSpace(value))
                {
                    m_TimeStart = DateTimeHelper.LongToDateTimeString(value);
                }
            }
        }

        public string CPN { get; set; }

        public string CDPN { get; set; }

        public string CPNName
        {
            get
            {
                return ShareInfo.NumberThinConfig.IpPhoneList.FirstOrDefault(p => p.CallNo1 == this.CPN)?.CallName;
            }
        }

        public string CDPNName
        {
            get
            {
                return ShareInfo.NumberThinConfig.IpPhoneList.FirstOrDefault(p => p.CallNo1 == this.CDPN)?.CallName;
            }
        }

        public int Duration
        {
            get { return (int)GetValue(DurationProperty); }
            set { SetValue(DurationProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Duration.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DurationProperty =
            DependencyProperty.Register("Duration", typeof(int), typeof(CallingRecord), new PropertyMetadata(0));



        public int Index
        {
            get { return (int)GetValue(IndexProperty); }
            set { SetValue(IndexProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Index.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IndexProperty =
            DependencyProperty.Register("Index", typeof(int), typeof(CallingRecord), new PropertyMetadata(0));
    }
}
